package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.entity.Channel;
import com.luhuiguo.cms.server.service.ChannelService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.rpc.CreateChannel;
import com.luhuiguo.cms.shared.rpc.CreateChannelResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class CreateChannelHandler implements ActionHandler<CreateChannel, CreateChannelResult> {

    @Autowired
    private ChannelService channelService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public CreateChannelResult execute(CreateChannel action, ExecutionContext context) throws ActionException {
        Channel c = MapperUtils.map(action.get(), Channel.class);

        channelService.saveChannel(c);

        return new CreateChannelResult(MapperUtils.map(c, ChannelModel.class));
    }

    @Override
    public Class<CreateChannel> getActionType() {
        return CreateChannel.class;
    }

    @Override
    public void rollback(CreateChannel action, CreateChannelResult result, ExecutionContext context)
            throws ActionException {

    }

}
